package cn.com.mall.product.service.impl;

import cn.com.mall.common.utils.PageUtils;
import cn.com.mall.common.utils.Query;
import cn.com.mall.product.dao.AttrGroupDao;
import cn.com.mall.product.entity.AttrGroupEntity;
import cn.com.mall.product.service.AttrGroupService;
import cn.com.mall.product.service.AttrService;
import cn.com.mall.product.vo.AttrVO;
import cn.com.mall.product.vo.SkuItemVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationServiceImpl attrAttrgroupRelationService;

    @Autowired
    AttrGroupService attrGroupService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }





    @Override
    public PageUtils queryPage(Map<String, Object> params, int catelogId) {

        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }

        if (catelogId == 0) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );

            return new PageUtils(page);
        }


        wrapper.eq("catelog_id",catelogId);

        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);

    }

    @Override
    public List<AttrGroupEntity> getAttrGroupsByCatlogId(Long catLogId) {
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catLogId);

        return this.getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public List<AttrVO> getAttrsByAttrGroupId(Long  attrGroupId) {


        return null;
    }

    @Override
    public List<SkuItemVo.SpuItemAttrGroupVo> getAttrGroupsBySpuId(Long spuId, Long catalogId) {
        //查询当前spu对应的所有属性分组信息以及当前分组下的所有属性数据
//
        return this.getBaseMapper().getAttrGroupWithAttrsBySpuId(spuId,catalogId);

    }


}